<script setup lang="ts">
import { OrderType } from '@/enums'
import type { ConsultOrderItem } from '@/types/consult'
import { useRouter } from 'vue-router'
import { ref } from 'vue'
import { delOrder } from '@/services/consult'
import { error, success } from '@/utils/tools'
import { useCancelOrder, useShowPrescription, useDelOrder } from '@/composable'
const prop = defineProps<{
  item: ConsultOrderItem
}>()
const router = useRouter()
// 订单状态对应的操作按钮
// 待支付：取消问诊+去支付
// 待接诊：取消问诊+继续沟通
// 咨询中：查看处方（如果开了）+继续沟通
// 已完成：更多（如果开了处方可以查看处方，删除订单）+问诊记录(聊天室)+（未评价?写评价:查看评价）
// 已取消：删除订单+咨询其他医生

// 更多的气泡参数
/* const showPopover = ref(false)
const actions = [
  { text: '查看处方', disabled: false },
  { text: '删除订单' }
]
// 点击气泡选项做的事情
const onSelect = (action: { text: string }, index: number) => {
  if (index === 1) {
    // 删除订单
    deleteOrder(prop.item)
  }
  if (index === 0) {
    // 查看处方
    showPrescription(prop.item.prescriptionId)
  }
} */

// 更多操作
const show = ref(false)



// 取消订单
const { loading, cancelConsultOrder } = useCancelOrder()
/* const loading = ref(false)
const cancelConsultOrder = async (item: ConsultOrderItem) => {
  loading.value = true
  try {
    await cancelOrder(item.id)
    // 正常应该去父组件去修改订单状态为已取消，但是因为我们传过来的item是对象，是引用传值，所以可以直接修改
    item.status = OrderType.ConsultCancel
    success('取消成功')
  } catch (err) {
    // todo
    error('取消失败')
  } finally {
    // 不管成功和失败都要走这里
    loading.value = false
  }
} */

// 删除订单
const edmit = defineEmits<{
  (e: 'on-delete', id: string): void
}>()

const { loading: deleteLoading, deleteOrder } = useDelOrder((id: string) => {
  edmit('on-delete', id)
})

/* const deleteLoading = ref(false)
const deleteOrder = async (item: ConsultOrderItem) => {
  deleteLoading.value = true
  try {
    delOrder(item.id)
    success('删除订单成功')
    // 通知父组件
    edmit('on-delete', item.id)
  } catch {
    error('删除订单失败')
  } finally {
    deleteLoading.value = false
  }
} */

// 查看处方
const { showPrescription } = useShowPrescription()


</script>

<template>
  <div class="consult-item">
    <div class="head van-hairline--bottom">
      <img class="img" src="@/assets/avatar-doctor.svg" />
      <p>{{ item.docInfo?.name || '暂未分配医生' }}</p>
      <!-- 文字颜色区分：代付款:黄色;沟通中:绿色;其他状态使用默认文字颜色-->
      <span :class="{
        orange: item.status === OrderType.ConsultPay,
        green: item.status === OrderType.ConsultChat
      }">{{ item.statusValue }}</span>
    </div>
    <div class="body" @click="router.push(`/user/consult/${item.id}`)">
      <div class="body-row">
        <div class="body-label">病情描述</div>
        <div class="body-value">{{ item.illnessDesc }}</div>
      </div>
      <div class="body-row">
        <div class="body-label">价格</div>
        <div class="body-value">¥ {{ item.payment.toFixed(2) }}</div>
      </div>
      <div class="body-row">
        <div class="body-label">创建时间</div>
        <div class="body-value tip">{{ item.createTime }}</div>
      </div>
    </div>
    <!-- 待支付：取消问诊+去支付 -->
    <div class="foot" v-if="item.status === OrderType.ConsultPay">
      <van-button :loading="loading" @click="cancelConsultOrder(item)" class="gray" plain size="small"
        round>取消问诊</van-button>
      <!-- 去订单详情页支付 -->
      <van-button @click="router.push(`/user/consult/${item.id}`)" type="primary" plain size="small"
        round>去支付</van-button>
    </div>
    <!-- 待接诊：取消问诊+继续沟通 -->
    <div class="foot" v-if="item.status === OrderType.ConsultWait">
      <van-button :loading="loading" @click="cancelConsultOrder(item)" class="gray" plain size="small"
        round>取消问诊</van-button>
      <!-- 去聊天室继续沟通 -->
      <van-button @click="router.push(`/room?orderId=${item.id}`)" type="primary" plain size="small"
        round>继续沟通</van-button>
    </div>
    <!-- 咨询中：查看处方（如果开了）+继续沟通-->
    <div class="foot" v-if="item.status === OrderType.ConsultChat">
      <van-button v-if="item.prescriptionId" class="gray" plain size="small" round
        @click="showPrescription(item.prescriptionId)">查看处方</van-button>
      <!-- 去聊天室继续沟通 -->
      <van-button @click="router.push(`/room?orderId=${item.id}`)" type="primary" plain size="small"
        round>继续沟通</van-button>
    </div>
    <!-- 已完成：更多（如果开了处方可以查看处方，删除订单）+问诊记录(聊天室)+（未评价?写评价:查看评价） -->
    <div class="foot" v-if="item.status === OrderType.ConsultComplete">
      <!-- 更多 -->
      <cp-consult-more @on-delete="deleteOrder(item)" @on-preview="showPrescription(item.prescriptionId)"
        v-model:show="show" :disabled="item.prescriptionId === undefined"></cp-consult-more>

      <van-button @click="router.push(`/room?orderId=${item.id}`)" class="gray" plain size="small" round>问诊记录</van-button>
      <van-button @click="router.push(`/room?orderId=${item.id}`)" v-if="item.evaluateId" class="gray" plain size="small"
        round>查看评价</van-button>
      <van-button @click="router.push(`/room?orderId=${item.id}`)" v-else class="green" type="primary" plain size="small"
        round>去评价</van-button>
    </div>
    <!-- 已取消：删除订单+咨询其他医生 -->
    <div class="foot" v-if="item.status === OrderType.ConsultCancel">
      <van-button :loading="deleteLoading" @click="deleteOrder(item)" class="gray" plain size="small"
        round>删除订单</van-button>
      <van-button @click="router.push('/')" type="primary" plain size="small" round>咨询其他医生</van-button>
    </div>
  </div>
</template>

<style lang="scss" scoped>
.consult-item {
  border-radius: 4px;
  box-shadow: 0px 0px 22px 0px rgba(245, 245, 245, 0.1);
  background-color: #fff;
  margin-bottom: 10px;

  .head {
    display: flex;
    align-items: center;
    height: 50px;
    padding: 0 15px;

    .img {
      width: 20px;
      height: 20px;
    }

    >p {
      flex: 1;
      font-size: 15px;
      padding-left: 10px;
    }

    >span {
      color: var(--cp-tag);

      &.orange {
        color: #f2994a;
      }

      &.green {
        color: var(--cp-primary);
      }
    }
  }

  .body {
    padding: 15px 15px 8px 15px;

    .body-row {
      display: flex;
      margin-bottom: 7px;
    }

    .body-label {
      width: 62px;
      font-size: 13px;
      color: var(--cp-tip);
    }

    .body-value {
      width: 250px;

      &.tip {
        color: var(--cp-tip);
      }
    }
  }

  .foot {
    padding: 0 15px 15px 15px;
    display: flex;
    justify-content: flex-end;
    align-items: center;

    .van-button {
      margin-left: 10px;
      padding: 0 16px;

      &.gray {
        color: var(--cp-text3);
        background-color: var(--cp-bg);
      }

      &--primary {
        color: #fff;
      }
    }

    /*  .more {
      color: var(--cp-tag);
      flex: 1;
      font-size: 13px;
    } */
  }
}
</style>